package com.che.software.testato.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.che.software.testato.business.IterationManager;
import com.che.software.testato.business.ProjectManager;
import com.che.software.testato.business.VersionManager;
import com.che.software.testato.domain.entity.Iteration;
import com.che.software.testato.domain.entity.search.IterationSearch;
import com.che.software.testato.domain.entity.search.ProjectSearch;
import com.che.software.testato.domain.entity.search.VersionSearch;
import com.che.software.testato.domain.enumeration.AssignmentStatus;
import com.che.software.testato.web.controller.acontroller.AbstractController;

/**
 * Controller used to manage the selective prioritizations.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see AbstractController, Serializable.
 * @since August, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Component("selectivePrioritizationsController")
@ManagedBean(name = "selectivePrioritizationsController")
@Scope("session")
@SessionScoped
public class SelectivePrioritizationsController extends AbstractController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(SelectivePrioritizationsController.class);
	private static final long serialVersionUID = -5958675470788053743L;

	/**
	 * Members.
	 */
	@Autowired
	private transient IterationManager iterationManager;
	private List<Iteration> selectivePrioritizations;
	@Autowired
	private transient ProjectManager projectManager;
	@Autowired
	private transient VersionManager versionManager;

	/**
	 * Method called to redirect to selective prioritizations page.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the url to follow.
	 * @since August, 2011.
	 */
	public String onSelectivePrioritizations() {
		LOGGER.debug("onSelectivePrioritizations().");
		selectivePrioritizations = null; // To trigger the data loading.
		return "selectivePrioritizations.xhtml?faces-redirect=true";
	}

	/**
	 * Getter for the private field value selectivePrioritizations.
	 * 
	 * @return the selectivePrioritizations field value.
	 */
	public List<Iteration> getSelectivePrioritizations() {
		if (null == selectivePrioritizations) {
			LOGGER.debug("getSelectivePrioritizations(): initialization.");
			IterationSearch searchBean = new IterationSearch();
			searchBean.setSelectiveStatus(AssignmentStatus.ENDED);
			try {
				List<Iteration> prioritizations = iterationManager.searchIteration(searchBean);
				for (Iteration prioritization : prioritizations) {
					prioritization.setVersion(versionManager.searchVersion(new VersionSearch(prioritization.getVersionId())).get(0));
					prioritization.setProject(projectManager.searchProject(new ProjectSearch(prioritization.getVersion().getProjectId())).get(0));
				}
				selectivePrioritizations = prioritizations;
			} catch (Exception e) {
				LOGGER.error("Error during the selective prioritizations recovery.", e);
			}
		}
		return selectivePrioritizations;
	}

	/**
	 * Setting a value to the selectivePrioritizations field.
	 * 
	 * @param selectivePrioritizations the value to set.
	 */
	public void setSelectivePrioritizations(List<Iteration> selectivePrioritizations) {
		this.selectivePrioritizations = selectivePrioritizations;
	}

	/**
	 * Getter for the private field value iterationManager.
	 * 
	 * @return the iterationManager field value.
	 */
	public IterationManager getIterationManager() {
		return iterationManager;
	}

	/**
	 * Setting a value to the iterationManager field.
	 * 
	 * @param iterationManager the value to set.
	 */
	public void setIterationManager(IterationManager iterationManager) {
		this.iterationManager = iterationManager;
	}

	/**
	 * Getter for the private field value projectManager.
	 * 
	 * @return the projectManager field value.
	 */
	public ProjectManager getProjectManager() {
		return projectManager;
	}

	/**
	 * Setting a value to the projectManager field.
	 * 
	 * @param projectManager the value to set.
	 */
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	/**
	 * Getter for the private field value versionManager.
	 * 
	 * @return the versionManager field value.
	 */
	public VersionManager getVersionManager() {
		return versionManager;
	}

	/**
	 * Setting a value to the versionManager field.
	 * 
	 * @param versionManager the value to set.
	 */
	public void setVersionManager(VersionManager versionManager) {
		this.versionManager = versionManager;
	}
}